package android.FiltrosJava;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

public class PolarCoordinates extends AbstractImageBasic{
	public BufferedImage applyFilter(BufferedImage dst) {
		BufferedImage src = copiaImagem(dst);
		
		int dimX = src.getWidth();
		int dimY = src.getHeight();
		double h, d, id, r;
		int xn, yn;
		int xCenter = (int) (dimX / 2.0f);
		int yCenter = (int) (dimY / 2.0f);
		
		WritableRaster raster_src = src.getRaster();
    	WritableRaster raster_dst = dst.getRaster();    	
    	int[] imageData = new int[raster_src.getNumBands()];
    	for (int x = 0; x < dimX; x++){
			for (int y = 0; y < dimY; y++){
				xn = x - xCenter;
				yn = y - yCenter;
				h = (float) Math.sqrt(xn * xn + yn * yn);
				r = Math.atan2(yn,xn);
				d = (float) (r * 180 / Math.PI + 90.0f);
				if (d > 360.0)
					d = d - 360.0;		

				id = (float) (360.0f - d);		

				xn = (int) (id * dimX / 360.0f);
				yn = (int) (h * dimY / 360.0f);

				if (xn >= dimX)
					xn = dimX - 1;

				if (yn >= dimY)
					yn = dimY - 1;
				
				raster_src.getPixel(xn,yn,imageData);	         		
	    		raster_dst.setPixel(x,y,imageData);	    		
			}
    	}
		src = null;
		return dst;		
	}
}